<?php
$title_page = "Thêm 1 sản phẩm";
require_once('include/permiss.php');
require_once('include/header.php');
require_once('include/menu.php');
require_once('config.php');

if (isset($_POST['submit'])) {
    $error = array();

    //Kiểm tra giá trị giỗng hay không	
    $arry = array('product_label' => 'Mã sản phẩm', 'product_name' => 'Tên sản phẩm', 'product_price' => 'Giá sản phẩm', 'product_time' => 'Thời gian');
    foreach ($arry as $key => $value) {
        if (!isset($_POST[$key]) || empty($_POST[$key])) {
            $error[] = "<font color=red>" . $value . "</font> chưa có thông tin";
        }
    }
    //Tìm nơi lưu ảnh
    $index_id = $_POST['index_id'];

    //Lỗi tải ảnh
    if ($_FILES["product_image"]["error"] == 0) {
        if (!($_FILES["product_image"]["type"] == "image/jpeg" || $_FILES["product_image"]["type"] == "image/gif" || $_FILES["product_image"]["type"] == "image/png")) {
            $error[] = "Ảnh phải có đuôi <font color=red>.jpg, .gif, hoặc .png</font>";
        } else {
            $product_image = time().$_FILES['product_image']['name'];
            move_uploaded_file($_FILES['product_image']['tmp_name'], '../image/data/' .$product_image);
        }
    }
    if (!count($error)) {
        $product_name = mysql_real_escape_string($_POST['product_name']);
        $product_label = mysql_real_escape_string($_POST['product_label']);
       
        $pdo = $conn->prepare('INSERT INTO datab_product(product_label, product_name, product_price, product_time, product_detail, index_id, product_image, product_adddate) VALUES(?, ?, ?, ?, ?, ?, ?, ?)');
        $pdo->execute(array($product_label, $product_name, $_POST['product_price'], $_POST['product_time'], $_POST['product_detail'], $_POST['index_id'], $product_image, date("Y-m-d H:i:s")));

        if ($pdo->rowCount()) {
            $message = 'Đã thêm sản phẩm thành công!';
        } else {
            $error[] = 'Không thể thêm sản phẩm';
        }
    }
}//hết isset submit
?>
<div class="container">

    <div class="box">
        <div class="box-heading">Sản phẩm</div>
        <div class="box-content">
            <ul>
                <?php
                if (!empty($error)) {
                    foreach ($error as $k) {
                        echo "<li style='color:red'>" . $k . "</li>";
                    }
                }
                if (isset($message)) {
                    echo '<li style=\'color:blue\'>' . $message . '</li>';
                }
                ?>
            </ul>
            <form method="post" enctype="multipart/form-data" action="">
                <table class="addlist">
                    <tr bgcolor="#EFEFEF">
                        <td class="modem">Mã*:</td>
                        <td class="name"><input type="text" name="product_label" size="40" value="<?php if (isset($product_label)) echo $product_label; ?>" /></td>
                    </tr>
                    <tr>
                        <td class="modem">Tên món*:</td>
                        <td class="name"><input type="text" name="product_name" size="40" value="<?php if (isset($product_name)) echo $product_name; ?>" /></td>
                    </tr>
                    <tr>
                        <td class="modem">Đơn giá*:</td>
                        <td class="name"><input type="text" name="product_price" size="5" value="<?php if (isset($product_price)) echo $product_price; ?>" />VNĐ</td>
                    </tr>
                    <tr>
                        <td class="modem">Thời gian làm*:</td>
                        <td class="name"><input type="text" name="product_time" size="3"value="<?php if (isset($product_time)) echo $product_time; ?>"/> phút</td>
                    </tr>

                    <tr>
                        <td class="modem">Thể loại*:</td>
                        <td class="name">
                            <select name="index_id">
                                <option value="">----</option>
                                <option value='1'>Món ăn nhanh</option>
                                <option value='2'>Món bánh</option>
                                <option value='3'>Đồ uống</option>
                            </select>
                        </td>
                    </tr>
                    <tr>
                        <td class="modem">Ảnh đại diện:</td>
                        <td class="name">
                            <span><input type="file" name="product_image" /></span></td>
                    </tr>
                    <tr>
                    <script type="text/javascript" src="/editor/ckeditor/ckeditor.js"></script>
                    <td class="modem">Mô tả:</td>
                    <td class="name">
                        <textarea class='ckeditor' name="product_detail"><?php if (isset($_POST['product_detail'])) echo $_POST['product_detail']; ?></textarea>
                    </td>
                    </tr>
                    <tr>
                        <td>&nbsp;</td>
                        <td>
                            <input type="submit" name="submit" value="Lưu" />
                        </td>
                    </tr>

                </table>
            </form>
        </div>
    </div>    
</div>
<?php
require_once('include/footer.php');
?>
 
